import { TemplateFilters } from './TemplateFilters';

export const Filters = new TemplateFilters();

// 数字转中文
function numberToChinese(num: number) {
    num = parseInt(String(num), 10);
    if (Number.isNaN(num) || num < 0 || num >= 100) {
        return String(num);
    }

    // 数字对应的汉字
    const digits = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const tens = ['', '十', '二十', '三十', '四十', '五十', '六十', '七十', '八十', '九十'];

    if (num < 10) {
        return digits[num];
    } if (num === 10) {
        return '十';
    } if (num < 20) {
        return `十${digits[num % 10]}`;
    }
    const ten = Math.floor(num / 10);
    const digit = num % 10;
    if (digit === 0) {
        return tens[ten];
    }
    return tens[ten] + digits[digit];
}

Filters.filter('ntc', numberToChinese);
